package com.qb.common.widget.titlebar;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import com.qb.baselibrary.R;

/**
 * titleBar
 */
public class BaseTitle extends RelativeLayout {
    private ImageView ib_titlebar_back,iv_right,img_bottom_line;
    private TextView tv_titlebar_title,tv_right;
    private RelativeLayout rl_titlebar_back;
    private Context mContext;
    public BaseTitle(Context context) {
        super(context);
        mContext = context;
        initView(context,null);
    }

    public BaseTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView(context,attrs);
    }

    public BaseTitle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context,attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public BaseTitle(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context,attrs);
    }

    private void initView(final Context context, AttributeSet attrs) {
        View titleView = LayoutInflater.from(context).inflate(R.layout.base_title,this);
        ib_titlebar_back = (ImageView) titleView.findViewById(R.id.ib_titlebar_back);
        iv_right = (ImageView) titleView.findViewById(R.id.iv_right);

        tv_titlebar_title = (TextView) titleView.findViewById(R.id.tv_titlebar_title);
        tv_right = (TextView) titleView.findViewById(R.id.tv_right);
        rl_titlebar_back = (RelativeLayout) titleView.findViewById(R.id.rl_titlebar_back);
    }



    public void setTitle(String text){
        tv_titlebar_title.setText(text);
    }

    public TextView getTitle(){
        return tv_titlebar_title;
    }
    public void setOnBack(){
        ib_titlebar_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity)mContext).finish();
            }
        });
    }
    /**
     * 获取左侧返回按键view
     * @return
     */
    public ImageView getLeftBack(){
        return ib_titlebar_back;
    }

    /**
     * 设置title色值
     * @param backColor
     */
    public void setTitleColor(String backColor){
        tv_titlebar_title.setTextColor(Color.parseColor(backColor));
    }

    /**
     * 设置整体背景颜色
     * @param backColor
     */
    public void setBackColor(String backColor){
        rl_titlebar_back.setBackgroundColor(Color.parseColor(backColor));
    }

    /**
     * 直接用int色值设置整体背景
     * @param backColor
     */
    public void setBackColor(int backColor){
        rl_titlebar_back.setBackgroundColor(backColor);
    }

    /**
     * 获取右侧ImageView
     * @return
     */
    public ImageView getRightImageview(){
        return iv_right;
    }

    /**
     * 获取右侧TextView
     * @return
     */
    public TextView getRightTextView(){
        return tv_right;
    }

    /**
     * 左右返回按钮需要特殊处理，例如点击返回触发某种事件
     * @param listener
     */
    public void setBackListener(OnClickListener listener){
        ib_titlebar_back.setOnClickListener(listener);
    }
    /**
     * 设置右侧TextView点击
     * @param content
     * @param listener
     */
    public void setRightTvListener(String content,OnClickListener listener){
        tv_right.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(content)){
            tv_right.setText(content);
        }
        tv_right.setOnClickListener(listener);
    }

    /**
     * 设置右侧ImageView点击
     * @param listener
     */
    public void setRightIvListener(OnClickListener listener){
        iv_right.setVisibility(View.VISIBLE);
        iv_right.setOnClickListener(listener);
    }

}
